<template>
    <view class="login-container">
        <view class="logo-container">
            <image class="logo" src="/static/images/logo.png" mode="aspectFill" />
        </view>
        <view class="title">配送端登录</view>
        <button class="login-btn" type="primary" @click="handleLogin">微信一键登录</button>
    </view>
</template>

<script setup>
import { useUserStore } from '@/store/user'
import { onShow } from '@dcloudio/uni-app'

const userStore = useUserStore()

/**
 * 页面显示时检查是否从顾客端返回
 */
onShow(() => {
  // 检查是否从其他小程序返回
  const referrerInfo = uni.getEnterOptionsSync().referrerInfo
  if (referrerInfo && referrerInfo.appId) {
    // 从顾客端返回，重新尝试登录检查openid状态
    console.log('从顾客端返回，重新检查登录状态')
    handleLogin()
  }
})

/**
 * 处理登录
 */
function handleLogin() {
    uni.showLoading({ title: '登录中...' })
    
    userStore.login().then(() => {
        uni.hideLoading()
        uni.showToast({ title: '登录成功', icon: 'success' })
        // 注意：页面跳转由 userStore.wxLogin 中的 checkDeliveryEntryStatus 函数根据配送员入驻状态自动处理
    }).catch((error) => {
        uni.hideLoading()
        console.error('登录失败:', error)
        uni.showToast({ title: '登录失败，请重试', icon: 'none' })
    })
}
</script>

<style lang="scss" scoped>
.login-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: #fff;
}

.logo-container {
    width: 200rpx;
    height: 200rpx;
    border-radius: 50%;
    overflow: hidden;
    margin-top: 80rpx;
    margin-bottom: 30rpx;
}

.logo {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.title {
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 40px;
    color: #333;
}

.login-btn {
    width: 80vw;
    max-width: 320px;
    margin-top: 20px;
    font-size: 18px;
}
</style>